/*******************************************************************************
 * Copyright (c) 2007 University of Oslo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     University of Oslo - initial API and implementation
 *******************************************************************************/

package com.cea.papyrus.diagram.sequence.sedi.figures;

import static java.lang.Math.max;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;

/**
 * @author Andreas Limyr
 * @author frankd@ifi.uio.no
 */
public class EOFigure extends AnchoringFigure {
    // extends InteractionFragmentFigure1
    // implements AnchorContributor {

    private Rectangle SIZE = new Rectangle(0, 0, 11, 1);

    protected void paintFigure(Graphics g) {
        Rectangle r = getBounds().getResized(-1, -1);
        g.setForegroundColor(this.getBorderColor());
        g.setBackgroundColor(this.getBackgroundColor());
        g.fillRectangle(r);
        g.drawRectangle(r);
    }

    /*
     * @Override public Rectangle getPreferredBounds(Rectangle suggestedBounds) { return
     * new Rectangle( suggestedBounds.x - SIZE.width / 2, suggestedBounds.y, SIZE.width,
     * Math.); }
     */

    // @Override - InteractionFigure1
    public Rectangle getPreferredModelBounds(final Rectangle bounds, final Rectangle coveredBounds) {
        if (coveredBounds != null) {
            return new Rectangle(coveredBounds.x + coveredBounds.width / 2, bounds.y, 1, max(
                    bounds.height, Grid.ANCHOR_DELTA));
        } else
            return bounds.getCopy();
    }

    // @Override - InteractionFigure1
    public Rectangle getPreferredFigureBounds(final Rectangle bounds, final Rectangle coveredBounds) {
        Rectangle preferredLocation = getPreferredModelBounds(bounds, coveredBounds);
        return new Rectangle(preferredLocation.x - SIZE.width / 2, preferredLocation.y, SIZE.width,
                preferredLocation.height);
    }

    // --- ILabeledFigure ---

    // Labels on EOs are currently not supported (but it is allowed in the
    // UML specification)

    public Rectangle getLabelBounds() {
        return getBounds().getCopy();
    }

    public void setLabel(String labelText) {
        // ignore
    }

    // --- Anchor contribution ---

    private AnchorPolicy anchorPolicy;

    public AnchorPolicy getAnchorPolicy() {
        if (anchorPolicy == null)
            anchorPolicy = new EOAnchorPolicy();
        return anchorPolicy;
    }

    public Rectangle getAnchorContributionBounds(IFigure figure) {
        if (figure instanceof EOFigure)
            return new Rectangle(0, 0, getSize().width, getSize().height);
        else {
            Rectangle ref = figure.getBounds();
            return getBounds().getTranslated(-ref.x, -ref.y).resize(0, Grid.RESERVE);
        }
    }
    
    

    public int[] contributeAnchors(IFigure covered, int y) {
        int midx = covered.getSize().width / 2;
        return new int[] {
                midx - getSize().width / 2, midx + getSize().width / 2
        };
    }
}
